import os
import os.path
import shutil
import logging

log = logging.getLogger(__name__)


def createDirectory(dirname, location, mode=0777):
    """
    Create a directory at the path location The function would raise an Exception if the file already
    or if the location could not be found and would return True if the process was a success 

    dirname: a string containing the name of the directory

    location: a string containing the full directory path

    mode:a interger containing the permission mode of the created file, default is 0777
    """
    
    requestPath = os.path.join(dirname, location)
    #check if directory already exists
    try:
        if os.path.isdir(requestPath):
            log.info("Directory already exists at " + location)
        else:
            os.mkdir(requestPath,mode)
        return True
    except:
        raise Exception("Could not create Directory")


def rename(oldPath,newPath):
    """
    Renames a directory or file.The function would raise an Exception if the file doesn't exist
    and would return True if process was a success 

    oldPath: a string containing the full path to the file needing renaming

    newPath: a string containing the new full path
    """    
    try: 
        #rename directory
        os.rename(oldPath,newPath)
        return True
    except:
        raise Exception("Could not rename")

def copyFile(oldFilePath,newFilePath):
    """
    Function copies a file located at "oldFilePath" into a file identified at "newFilePath"
    file the function would raise an exception if the file at "oldFilePath" doesn't exist and
    would return True if process was a success. 

    oldFilePath: a string containing the full path to the source file to be copied
 
    newFilePath: a string containing the full path of the copy destination
    """
    
    if os.path.isfile(oldFilePath)==False:
        raise Exception ("File: "+oldFilePath+" doesn't exist")
    try:
        shutil.copy2(oldFilePath,newFilePath)
        return True
    except:
        raise Exception("Could not copy file")

def removeFile(filePath):
    """
    removes a file at filePath
    
    filePath: a string containing the full path of the file to be deleted
    """
    
    if os.path.isfile(filePath)==False:
        raise Exception ("File: "+filePath+" doesn't exist")
    try:
        os.remove(filePath)
        return True
    except:
        raise Exception("Could not remove file")


def removeDir(directoryPath):
    """
    Recursively removes the directory and files contained in the directory

    directoryPath: a string contianing the full path to the directory to be deleted

    caution removeDir("/") could potentially remove your whold harddisk
    """

    if os.path.isdir(directoryPath)==False:
        raise Exception ("Directory: "+directoryPath+" doesn't exist")
    try:
        for root, dirs, files in os.walk(directoryPath, topdown=False):
            for name in files:
                os.remove(os.path.join(root, name))
            for name in dirs:
                os.rmdir(os.path.join(root, name))

        #remove the directory
        os.rmdir(directoryPath)
        return True
    except:
        raise Exception("Could not remove directory")


